package com.fypg.module.medical.controller.admin.outpatientorderrefund;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.fypg.framework.common.pojo.PageParam;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.pojo.CommonResult;
import com.fypg.framework.common.util.object.BeanUtils;
import static com.fypg.framework.common.pojo.CommonResult.success;

import com.fypg.framework.excel.core.util.ExcelUtils;

import com.fypg.framework.apilog.core.annotation.ApiAccessLog;
import static com.fypg.framework.apilog.core.enums.OperateTypeEnum.*;

import com.fypg.module.medical.controller.admin.outpatientorderrefund.vo.*;
import com.fypg.module.medical.dal.dataobject.outpatientorderrefund.OutpatientOrderRefundDO;
import com.fypg.module.medical.service.outpatientorderrefund.OutpatientOrderRefundService;

@Tag(name = "管理后台 - 门诊订单退款信息")
@RestController
@RequestMapping("/medical/outpatient-order-refund")
@Validated
public class OutpatientOrderRefundController {

    @Resource
    private OutpatientOrderRefundService outpatientOrderRefundService;

    @PostMapping("/create")
    @Operation(summary = "创建门诊订单退款信息")
    @PreAuthorize("@ss.hasPermission('medical:outpatient-order-refund:create')")
    public CommonResult<String> createOutpatientOrderRefund(@Valid @RequestBody OutpatientOrderRefundSaveReqVO createReqVO) {
        return success(outpatientOrderRefundService.createOutpatientOrderRefund(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新门诊订单退款信息")
    @PreAuthorize("@ss.hasPermission('medical:outpatient-order-refund:update')")
    public CommonResult<Boolean> updateOutpatientOrderRefund(@Valid @RequestBody OutpatientOrderRefundSaveReqVO updateReqVO) {
        outpatientOrderRefundService.updateOutpatientOrderRefund(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除门诊订单退款信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('medical:outpatient-order-refund:delete')")
    public CommonResult<Boolean> deleteOutpatientOrderRefund(@RequestParam("id") String id) {
        outpatientOrderRefundService.deleteOutpatientOrderRefund(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除门诊订单退款信息")
                @PreAuthorize("@ss.hasPermission('medical:outpatient-order-refund:delete')")
    public CommonResult<Boolean> deleteOutpatientOrderRefundList(@RequestParam("ids") List<String> ids) {
        outpatientOrderRefundService.deleteOutpatientOrderRefundListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得门诊订单退款信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('medical:outpatient-order-refund:query')")
    public CommonResult<OutpatientOrderRefundRespVO> getOutpatientOrderRefund(@RequestParam("id") String id) {
        OutpatientOrderRefundDO outpatientOrderRefund = outpatientOrderRefundService.getOutpatientOrderRefund(id);
        return success(BeanUtils.toBean(outpatientOrderRefund, OutpatientOrderRefundRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得门诊订单退款信息分页")
    @PreAuthorize("@ss.hasPermission('medical:outpatient-order-refund:query')")
    public CommonResult<PageResult<OutpatientOrderRefundRespVO>> getOutpatientOrderRefundPage(@Valid OutpatientOrderRefundPageReqVO pageReqVO) {
        PageResult<OutpatientOrderRefundDO> pageResult = outpatientOrderRefundService.getOutpatientOrderRefundPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OutpatientOrderRefundRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出门诊订单退款信息 Excel")
    @PreAuthorize("@ss.hasPermission('medical:outpatient-order-refund:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOutpatientOrderRefundExcel(@Valid OutpatientOrderRefundPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OutpatientOrderRefundDO> list = outpatientOrderRefundService.getOutpatientOrderRefundPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "门诊订单退款信息.xls", "数据", OutpatientOrderRefundRespVO.class,
                        BeanUtils.toBean(list, OutpatientOrderRefundRespVO.class));
    }

}